// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "user_id": {
    "rules": [
      {
        "format": "string",
        "required": true
      }
    ],
    "title": "发起用户",
    "label": "发起用户"
  },
  "verification_code": {
    "rules": [
      {
        "format": "string",
        "required": true
      }
    ],
    "title": "动态密码",
    "label": "动态密码"
  },
  "operator_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "操作人员",
    "label": "操作人员"
  },
  "deposit_account_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "提款账户",
    "label": "提款账户"
  },
  "withdraw_amount": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "double"
      }
    ],
    "title": "提现金额",
    "label": "提现金额"
  },
  "withdraw_remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "提现附言",
    "label": "提现附言"
  },
  "operator_remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "操作备注",
    "label": "操作备注"
  },
  "withdraw_status": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [{
          "value": 0,
          "text": "出款成功"
        }, {
          "value": 1,
          "text": "正在审核"
        },{
          "value": 2,
          "text": "提款拒绝"
        },{
          "value": 3,
          "text": "正在出款"
        },{
          "value": 4,
          "text": "出款失败"
        }]
      }
    ],
    "title": "提现状态",
    "defaultValue": 0,
    "label": "提现状态"
  },
  "terminal_type": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "value": 0,
            "text": "商户"
          },
          {
            "value": 1,
            "text": "骑手"
          }
        ]
      }
    ],
    "title": "终端类型",
    "defaultValue": 0,
    "label": "终端类型"
  },
  "last_update_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "最后更新时间",
    "label": "最后更新时间"
  }
}

const enumConverter = {
  "withdraw_status_valuetotext": {
    "0": "出款成功",
    "1": "正在审核",
    "2": "提款拒绝",
    "3": "正在出款",
    "4": "出款失败"
  },
  "terminal_type_valuetotext": {
    "0": "商户",
    "1": "骑手"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
